
$(function() {
	Tip.emp('考试人员');
});

var ExamEmp = {
		
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'exam.php?do=ExamEmp.edit&id=' + id;
		var c2 = 'exam.php?do=ExamEmp.view&id=' + id;
		
		var a1 = ['答题情况', 'url', 'ExamEmp.dialog_edit_', '700x550'];
		var a2 = ['查看试卷', 'url', 'Common.dialog_view_', '700x550'];
		
		if (status == 1 || status == 2) {
			var s = c2;
			return this.popup_(e, [a2], s);
		}
		
		if (status == 3 || status == 4) {
			var s = c1;
			return this.popup_(e, [a1], s);
		}
		
		var s = c2;
		return this.popup_(e, [a2], s);		
	},
	
	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var ss = 'exam.php?do=ExamEmp.ss&id=' + id;
		var bb = ['修改状态', 'url', 'ExamEmp.dialog_ss', '400x200'];
		
		s += ", " + ss;
		a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('exam.php?do=ExamEmp.audit&item=77', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'exam.php?do=ExamEmp.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_edit_: function (url, title, xy) {
		$('#dialog_edit').dialog('option', 'buttons', {	
			"完成阅卷": function() { 
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				ExamEmp.decide(5);	
			},
			
			"保存": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				ExamEmp.decide(4);
			},
			
			"开始阅卷": function() { 
				var url = 'exam.php?do=ExamEmp.start';
	
				var form = $("#form_edit")[0];
				
				var body = { };
				body.id = form.id.value;
				body.status = 4;
				
				$.getJSON(url, body, function(json) {
					if (json.status == 1) {
						alert("提交成功，开始阅卷");
					} else {
						alert("用户还未交卷，不能开始阅卷");
					}
				});
			},
		});
	
		dialog_edit(url, title, xy);
	},
	
	decide: function(status) {
		var result = Common.result();
		var form = $("#form_edit")[0];
		
		var body = { };
		body.id = form.id.value;
		body.total = form.total.value;
		
		var score = 0;
						
		var a = [];
		$("#form_edit input[type='hidden']").each(function() {
			if (this.name == 'id' || this.name == 'total') {
				// alert(this.name);
				// continue;
				var sss = '22';
			} else {
				var v = $(this).val();	
				if (v != '') {
					score += parseFloat(v);
				}
				// alert(v);
				a.push({name: this.name, value: v});
			}			
		});
		
		$("#form_edit :text").each(function() {
			var v = $(this).val();
			if (v != '') {
				score += parseFloat(v);
			}
			
			a.push({name: this.name, value: v});
		});
		
		body.score = score;
		body.score_a = a;
		body.status = status;
		
		// alert($.toJSON(body));
		
		$('#dialog_edit').dialog("close");

		var url = 'exam.php?do=ExamEmp.editPost';
		var p2 = $.cookie('p2');
		if (p2 != null) {
			url += p2;
		}

		$.post(url, body, function(data) {
			$(result).html(data);
		});
	}
};
